package com.euauth;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class EuSign {
	public static String hmacSign(String key, String data) throws NoSuchAlgorithmException, InvalidKeyException, IllegalStateException, UnsupportedEncodingException {
		SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA1");
	    Mac mac = Mac.getInstance("HmacSHA1");
	    mac.init(secret_key);
	    byte[] raw = mac.doFinal(data.getBytes("UTF-8"));
	    return bytesToHex(raw);
	}

	public static String bytesToHex(byte[] b) {
		String result = "";
	    for (int i = 0; i < b.length; i++) {
	      result = result + Integer.toString((b[i] & 0xFF) + 256, 16).substring(1);
	    }
	    return result;
	}
}
